<?PHP
class Page{
  public $id      = -1;
  public $alias   = '';
  public $title   = '';
  public $ownerid = -1;
  public $owner   = false;
  public $menu    = -1;
  public $meta    = null;
  public $editor  = null;
  
  function loadProperties($alias){
    if ($alias == "") {
      $alias = "home";
    }
	$row = $GLOBALS['db']->ReadRow("SELECT * FROM {'dbprefix'}pages 
                           WHERE alias = '".$alias."'");
    $this->load($row);
  }
  
  function loadPropertiesById($id){
	$row = $GLOBALS['db']->ReadRow("SELECT * FROM {'dbprefix'}pages 
                           WHERE id = '".$id."'");
    $this->load($row);
  }
  
  function load($data){
    if($data){
      $this->id = $data->id;
      $this->title = $data->title;
      $this->ownerid = $data->owner;
      $this->menu = $data->menu;
      $this->alias = $data->alias;
	  $this->meta  = new Meta();
	  $this->meta->pageid = $this->id;
	  $this->meta->load();
	  $this->editor = new $data->editor();
	  $this->editor->page = $this;
    }
  }

  function getContent(){
    EventManager::raiseEvent("content_top",".",$this);
    $this->editor->display();
    EventManager::raiseEvent("content_bottom",".",$this);
  }

  function getOwner(){
    if(!$this->owner){
      $alias = $GLOBALS['db']->ReadField("SELECT alias FROM {'dbprefix'}pages 
                                          WHERE id = '".$this->ownerid."'");
      if($alias){
        $this->owner = new Page();
        $this->owner->loadProperties($alias);
      }
    }
    return $this->owner;
  }

  function getBreadcrump(){
    if(!$this->owner) $this->getOwner();
    if($this->owner){
      $breadcrump = $this->owner->getBreadcrump();
    }
    $breadcrump[] = array($this->alias,$this->title);
    return $breadcrump;
  }

  function save(){
	$res = $GLOBALS['db']->Execute("UPDATE {'dbprefix'}pages SET
                                    title = '".$this->title."',
                                    alias = '".$this->alias."',
                                    menu = '".$this->menu."' 
                                    WHERE id = '".$this->id."'");
	$this->meta->save();
	if($res){
	  $args['title'] = $this->title;
	  $args['alias'] = $this->alias;
	  $args['menu']  = $this->menu;
	  $args['id']    = $this->id;
      EventManager::raiseEvent("page_saved","../",$args);
	}
	return $res;
  }

  function create($dirpraefix,$alias,$editor){
    $res = $GLOBALS['db']->Execute("INSERT INTO {'dbprefix'}pages (alias,editor) VALUES ('".$alias."','".$editor."')");
	if($res){
	  $args['id']     = $GLOBALS['db']->InsertID();
      $args['editor'] = $editor;
	  $args['alias']  = $alias;
      EventManager::raiseEvent("page_created","../",$args);
	}
    return $GLOBALS['db']->InsertID();
  }

  function delete(){
    $res = $GLOBALS['db']->Execute("DELETE FROM {'dbprefix'}pages WHERE alias = '".$this->alias."'");
	if($res){
	  $args['alias'] = $this->alias;
      EventManager::raiseEvent("page_deleted","../",$args);
	}
    return $res;
  }

  function getPagesTypes(){
    return $GLOBALS['db']->ReadRows("SELECT * FROM {'dbprefix'}pagetypes");
  }
  
  function getPagesByDir($dir){
    $dir = $GLOBALS['db']->EscapeString($dir);
	if(substr($dir,0,1) == "/"){
	  $dir = substr($dir,1);
	}
	return $GLOBALS['db']->ReadRows("SELECT * FROM {'dbprefix'}pages 
                           WHERE alias LIKE '".$dir."%'");
    
  }
}

?>
